<?php
require_once 'inc/util.php';

$facebook = fb_authenticate();
$user = $facebook->getUser();

if (!isset($user_details)) {
    $user_details = get_user_details($user, $facebook, "friends", $facebook->getAccessToken());
}

if (isset($_GET["action"]) && ((strcmp($_GET["action"], "write") == 0))) {
    $filename = "/tmp/labels";
    $label_file = fopen($filename, 'wt');
    $n_friends = isset($_GET["limit"]) ? $_GET["limit"] : get_num_friends($user, $facebook, $user_details);
    for ($i = 1; $i <= $n_friends; $i++) {
        fwrite($label_file, $_POST["$i"] . "\n");
    }

    // Extract the type of file which will be sent to the browser as a header
    $type = filetype($filename);

    // Get a date and timestamp
    date_default_timezone_set('UTC');
    $today = date("F j, Y, g:i a");
    $time = time();

    // Send file headers
    header("Content-type: text/plain");
    header("Content-Disposition: attachment;filename=$filename");
    header("Content-Transfer-Encoding: binary");
    header('Pragma: no-cache');
    header('Expires: 0');
    // Send the file contents.
    set_time_limit(0);
    readfile($filename);
} else {

    $friends_list = get_friend_names($user, $facebook, $user_details);
?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Manually Label your Friends</title>

            <script type="text/javascript" src="js/utils.js"></script>

        </head>
        <body>
            <p style="text-align: center">Just assign a value to your friends based on your relationship with them</p>
            <div style="text-align: center">
                <form action="label.php" method="POST">
                    Limit to: <input type="text" name="limit" /> <input type="submit" value="Submit" />
                </form>
            </div>
            <form action="label.php?action=write<?php if (isset($_POST["limit"]))
        echo '&limit=' . $_POST["limit"] ?>" method="POST">
                    <table align="center">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>Relationship</th>
                            </tr>
                        </thead>
                <?php
                $i = 1;

                foreach ($friends_list as $friend) {
                    if (isset($_POST["limit"])) {
                        if ($i > $_POST["limit"]) {
                            break;
                        }
                    }
                ?>
                    <tr>
                        <td><?php echo $friend ?></td>
                        <td><input type="range" min="1" max="5" name="<?php echo $i ?>" onchange="showValue('<?php echo 'd' . $i ?>',this.value)"/></td>
                        <td>
                            <div style="float:right" id="<?php echo 'd' . $i ?>">
                                We know each other
                            </div>
                        </td>
                    </tr>

                <?php
                    $i++;
                }
                ?>
            </table>
            <div style="text-align:center">
                <input type="submit" value="Submit" />
            </div>
        </form>

    </body>
</html>
<?php
            }
?>